<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/12/13 14:41:48 $)
// $RCSfile: functions_tickets.php,v $ : $Revision: 1.18 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Inserts a new Attachment Type
*/
function insertAttachmentType($extension, $maxsize, $mimetype)
{
	global $dbCore;

	$maxsize = intval($maxsize);
	if (!$maxsize)
	{
		return false;
	}

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."attachmenttypes` (`extension`, `maxsize`, `mimetype`) VALUES ('". $dbCore->escape(strtolower($extension)) ."', '". intval($maxsize) ."', '". $dbCore->escape(strtolower($mimetype)) ."');");
	$attachmenttypeid = $dbCore->lastInsertId();

	rebuildAttachmentTypeCache();

	return $attachmenttypeid;
}

/**
* Updates a given attachment type
*/
function updateAttachmentType($attachmenttypeid, $extension, $maxsize, $mimetype)
{
	global $dbCore;

	$maxsize = intval($maxsize);
	if (!$maxsize)
	{
		return false;
	}

	$dbCore->query("UPDATE `". TABLE_PREFIX ."attachmenttypes` SET `extension` = '". $dbCore->escape(strtolower($extension)) ."', `maxsize` = '". intval($maxsize) ."', `mimetype` = '". $dbCore->escape(strtolower($mimetype)) ."' WHERE `attachmenttypeid` = '". intval($attachmenttypeid) ."';");

	return true;
}

/**
* Deletes the attachment types specified in the attachmenttypeidarray
*/
function deleteAttachmentTypes($attachmenttypeidarray)
{
	global $dbCore;

	if (!_is_array($attachmenttypeidarray))
	{
		return false;
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."attachmenttypes` WHERE `attachmenttypeid` IN (". buildIN($attachmenttypeidarray) .");");

	rebuildAttachmentTypeCache();

	return true;
}

/**
* Gets total number of ticket status
*/
function getStatusCount()
{
	global $_SWIFT, $dbCore, $datastore;
	
	$datastore->load("statuscache");

	return count($_SWIFT["statuscache"]);
}

/**
* Gets the display order number for the next insert
*/
function getLastStatusDisplayOrder()
{
	global $_SWIFT, $dbCore, $datastore;

	$datastore->load("statuscache");

	// Get Last Insert ID
	$linsertid = max(array_keys($_SWIFT["statuscache"]));
	
	$disporder = $_SWIFT["statuscache"][$linsertid]["displayorder"]+1;
	return $disporder;
}

/**
* Gets the display order number for the next insert
*/
function getLastPriorityDisplayOrder()
{
	global $_SWIFT, $dbCore, $datastore;

	$datastore->load("prioritycache");

	// Get Last Insert ID
	$linsertid = max(array_keys($_SWIFT["prioritycache"]));
	
	$disporder = $_SWIFT["prioritycache"][$linsertid]["displayorder"]+1;
	return $disporder;
}

/**
* Inserts a new status into the database
*/
function insertStatus($title, $displayorder, $displayinmainlist, $displaycount, $statuscolor, $departmentid, $type, $resetduetime)
{
	global $dbCore, $_SWIFT;

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."ticketstatus` (`title`, `displayorder`, `iscustom`, `displayinmainlist`, `displaycount`, `statuscolor`, `departmentid`, `type`, `resetduetime`) VALUES ('". $dbCore->escape($title) ."', '". intval($displayorder) ."', '1', '". intval($displayinmainlist) ."', '". intval($displaycount) ."', '". $dbCore->escape($statuscolor) ."', '". intval($departmentid) ."', '". $dbCore->escape($type) ."', '". intval($resetduetime) ."');", 3);
	$statusid = $dbCore->lastInsertId();

	rebuildStatusCache();

	return $statusid;
}

/**
* Update existing ticket status
*/
function updateTicketStatusType($ticketstatusid, $title, $displayorder, $displayinmainlist, $displaycount, $statuscolor, $departmentid, $type, $resetduetime)
{
	global $dbCore, $_SWIFT;

	$dbCore->query("UPDATE `". TABLE_PREFIX ."ticketstatus` SET `title` = '". $dbCore->escape($title) ."', `displayorder` = '". intval($displayorder) ."', `displayinmainlist` = '". intval($displayinmainlist) ."', `displaycount` = '". intval($displaycount) ."', `statuscolor` = '". $dbCore->escape($statuscolor) ."', `departmentid` = '". intval($departmentid) ."', `type` = '". $dbCore->escape($type) ."', `resetduetime` = '". intval($resetduetime) ."' WHERE `ticketstatusid` = '". intval($ticketstatusid) ."';", 3);

	rebuildStatusCache();

	return $statusid;
}

/**
* Deletes a status from a given statusid
*/
function deleteStatus($statusidarray)
{
	global $dbCore, $_SWIFT;

	if (!is_array($statusidarray))
	{
		return false;
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."ticketstatus` WHERE `ticketstatusid` IN(". buildIN($statusidarray) .");");

	/**
	* ###############################################
	* RESET TICKET STATUS ID IN TICKET TABLES
	* ###############################################
	*/

	rebuildStatusCache();

	return true;
}

/**
* Inserts a new priority into the database
*/
function insertPriority($title, $displayorder, $type, $tgroupidlist, $frcolorcode = '', $bgcolorcode = '')
{
	global $dbCore, $_SWIFT;
	
	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."ticketpriorities` (`title`, `displayorder`, `type`, `frcolorcode`, `bgcolorcode`, `iscustom`) VALUES('". $dbCore->escape($title) ."', '". $dbCore->escape($displayorder) ."', '". $dbCore->escape($type) ."', '". $dbCore->escape($frcolorcode) ."', '". $dbCore->escape($bgcolorcode) ."', '1');");
	$priorityid = $dbCore->lastInsertId();
	if (!$priorityid)
	{
		return false;
	}

	insertTemplateGroupAssignLink($tgroupidlist, TGROUP_PRIORITY, $priorityid, false);

	rebuildPriorityCache();

	return $priorityid;
}

/**
* Update Existing Ticket Priority
*/
function updateTicketPriority($priorityid, $title, $displayorder, $type, $tgroupidlist, $frcolorcode = '', $bgcolorcode = '')
{
	global $dbCore, $_SWIFT;
	
	$dbCore->query("UPDATE `". TABLE_PREFIX ."ticketpriorities` SET `title` = '". $dbCore->escape($title) ."', `displayorder` = '". $dbCore->escape($displayorder) ."', `type` = '". $dbCore->escape($type) ."', `frcolorcode` = '". $dbCore->escape($frcolorcode) ."', `bgcolorcode` = '". $dbCore->escape($bgcolorcode) ."' WHERE `priorityid` = '". intval($priorityid) ."';");

	insertTemplateGroupAssignLink($tgroupidlist, TGROUP_PRIORITY, $priorityid, true);

	rebuildPriorityCache();

	return $priorityid;
}

/**
* Deletes a given priority
*/
function deletePriority($priorityidarray)
{
	global $dbCore, $_SWIFT;

	if (!is_array($priorityidarray))
	{
		return false;
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."ticketpriorities` WHERE `priorityid` IN (". buildIN($priorityidarray) .");");

	deleteTemplateGroupAssignLinks($priorityidarray, TGROUP_PRIORITY);

	/**
	* ###############################################
	* RESET TICKET PRIORITIES IN TICKET TABLES
	* ###############################################
	*/

	rebuildPriorityCache();

	return true;
}

/**
* Rebuilds the ticket status cache
*/
function rebuildStatusCache()
{
	global $dbCore, $_SWIFT, $datastore;

	$statuscache = array();
	
	$statusindex = 0;
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."ticketstatus` ORDER BY `displayorder` ASC;", 3);
	while ($dbCore->nextRecord(3))
	{
		$statusindex++;
		$statuscache[$dbCore->Record3["ticketstatusid"]] = $dbCore->Record3;
		$statuscache[$dbCore->Record3["ticketstatusid"]]["index"] = $statusindex;
	}

	$datastore->update("statuscache", $statuscache);
}

/**
* Rebuilds the ticket priority cache
*/
function rebuildPriorityCache()
{
	global $dbCore, $_SWIFT, $datastore;

	$prioritycache = array();
	
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."ticketpriorities` ORDER BY `displayorder` ASC;", 3);
	while ($dbCore->nextRecord(3))
	{
		$prioritycache[$dbCore->Record3["priorityid"]] = $dbCore->Record3;
	}

	$datastore->update("prioritycache", $prioritycache);
}

/**
* Rebuilds the Allowed Attachment Type Cache
*/
function rebuildAttachmentTypeCache()
{
	global $dbCore, $_SWIFT, $datastore;

	$attachmenttypescache = array();
	
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."attachmenttypes` ORDER BY `attachmenttypeid` ASC;", 3);
	while ($dbCore->nextRecord(3))
	{
		$attachmenttypescache[] = $dbCore->Record3["extension"];
	}

	$datastore->update("attachmenttypescache", $attachmenttypescache);	
}

/**
* Render the Ticket Status Insert/Edit Form
*/
function renderTicketStatusForm($type)
{
	global $dbCore, $_SWIFT;

	printFormStart();
	printMainTableHeader($_SWIFT["language"]["statusdetails"]);

	printTextRow("title", $_SWIFT["language"]["statustitle"], $_SWIFT["language"]["desc_statustitle"], "text", $_POST["title"]);

	$options = array();
	$index = 1;
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."departments` ORDER BY `displayorder` ASC;");
	$options[0]["title"] = $_SWIFT["language"]["statusalldep"];
	$options[0]["value"] = 0;
	while ($dbCore->nextRecord())
	{
		if ($dbCore->Record["departmentmodule"] == MODULE_TICKETS)
		{
			$options[$index]["title"] = $dbCore->Record["title"];
			$options[$index]["value"] = $dbCore->Record["departmentid"];
			$index++;
		}
	}
	printSelectRow("departmentid", $_SWIFT["language"]["statusdep"], $_SWIFT["language"]["desc_statusdep"], $options);

	printColorRow("statuscolor", $_SWIFT["language"]["statuscolor"], $_SWIFT["language"]["desc_statuscolor"], $_POST["statuscolor"]);
	if ($type == INSERT)
	{
		$_displayorder = getLastStatusDisplayOrder();
	} else {
		$_displayorder = $_POST["displayorder"];
	}
	printNumberRow("displayorder", $_SWIFT["language"]["displayorder"], $_SWIFT["language"]["desc_displayorder"], $_displayorder);

	if ($type == INSERT)
	{
		$_displayinmainlist = true;
	} else {
		if ($_POST["displayinmainlist"] == 1)
		{
			$_displayinmainlist = true;
		} else {
			$_displayinmainlist = false;
		}
	}
	printYesNoRow("displayinmainlist", $_SWIFT["language"]["displayinmainlist"], $_SWIFT["language"]["desc_displayinmainlist"], $_displayinmailist);

	if ($type == INSERT)
	{
		$_displaycount = true;
	} else {
		if ($_POST["displaycount"] == 1)
		{
			$_displaycount = true;
		} else {
			$_displaycount = false;
		}
	}
	printYesNoRow("displaycount", $_SWIFT["language"]["displaycount"], $_SWIFT["language"]["desc_displaycount"], $_displaycount);

	if ($type == INSERT)
	{
		$_type = true;
	} else {
		$_type = iif($_POST["type"]==SWIFTPUBLIC, true, false);
	}
	printPublicPrivateRow("type", $_SWIFT["language"]["statustype2"], $_SWIFT["language"]["desc_statustype2"], $_type);


	if ($type == INSERT)
	{
		$_resetduetime = false;
	} else {
		if ($_POST["resetduetime"] == 1)
		{
			$_displaycount = true;
		} else {
			$_displaycount = false;
		}
	}
	printYesNoRow("resetduetime", $_SWIFT["language"]["resetduetime"], $_SWIFT["language"]["desc_resetduetime"], $_resetduetime);

	printSubmitRow(iif($type==INSERT, $_SWIFT["language"]["insertstatus"], $_SWIFT["language"]["updatestatus"]));

	printMainTableFooter();

	addHiddenField("_m", "tickets");
	addHiddenField("_a", iif($type==INSERT, "insertstatus", "editstatus"));
	addHiddenField("step", "1");
	if ($type == EDIT)
	{
		addHiddenField("ticketstatusid", $_POST["ticketstatusid"]);		
	}
	printFormEnd();
}

/**
* Render the Ticket Priority Insert/Edit Form
*/
function renderTicketPriorityForm($type)
{
	global $dbCore, $_SWIFT;

	printFormStart();
	printMainTableHeader($_SWIFT["language"]["prioritydetails"]);

	printTextRow("title", $_SWIFT["language"]["prioritytitle"], $_SWIFT["language"]["desc_prioritytitle"], "text", $_POST["title"]);
	
	if ($type == INSERT)
	{
		$_displayorder = getLastPriorityDisplayOrder();
	} else {
		$_displayorder = $_POST["displayorder"];
	}

	printNumberRow("displayorder", $_SWIFT["language"]["displayorder"], $_SWIFT["language"]["desc_displayorder"], $_displayorder);

	if ($type == INSERT)
	{
		$_type = true;
	} else {
		$_type = iif($_POST["type"]==SWIFTPUBLIC, true, false);
	}
	printPublicPrivateRow("type", $_SWIFT["language"]["prioritytype"], $_SWIFT["language"]["desc_prioritytype"], $_type);
	
	printColorRow("frcolorcode", $_SWIFT["language"]["forecolor"], $_SWIFT["language"]["desc_forecolor"], $_POST["frcolorcode"]);
	printColorRow("bgcolorcode", $_SWIFT["language"]["bgcolor"], $_SWIFT["language"]["desc_bgcolor"], $_POST["bgcolorcode"]);

	printTGroupRow("tgroupid", $_SWIFT["language"]["templategroups"], $_SWIFT["language"]["desc_templategroups"], $type, TGROUP_PRIORITY, $_POST["priorityid"]);

	printSubmitRow(iif($type==INSERT, $_SWIFT["language"]["insertpriority"], $_SWIFT["language"]["updatepriority"]));

	printMainTableFooter();

	addHiddenField("_m", "tickets");
	addHiddenField("_a", iif($type==INSERT, "insertpriority", "editpriority"));
	addHiddenField("step", "1");
	if ($type == EDIT)
	{
		addHiddenField("priorityid", $_POST["priorityid"]);		
	}
	printFormEnd();
}
?>